package cn.me.alphamq.common.entity.req;

import cn.me.alphamq.common.entity.BaseMsg;
import cn.me.alphamq.common.entity.Message;
import cn.me.alphamq.common.enumeration.LocalTxStateEnum;
import cn.me.alphamq.common.enumeration.TxMsgFlagEnum;

/**
 * 发送事务消息的请求
 *
 * @author f
 */
public class PushTxMsgReq extends BaseMsg {

    /**
     * 消息的主题
     */
    private String topic;

    /**
     * 放入消息的队列 id，默认值是 -1，如果没有传递，则会在写入数据时默认写到 0 号队列
     */
    private int queueId;

    /**
     * 消息的主体
     */
    private byte[] body;

    /**
     * 能够唯一标识生产者的 id
     */
    private String producerId;

    /**
     * 事务的标志
     *
     * @see TxMsgFlagEnum
     */
    private int txFlag;

    /**
     * 事务的状态
     *
     * @see cn.me.alphamq.common.enumeration.LocalTxStateEnum
     */
    private int transactionState;

    public PushTxMsgReq() {
    }

    private PushTxMsgReq(Message message, String msgId, String producerId,
                        TxMsgFlagEnum txMsgFlag, LocalTxStateEnum localTxState) {
        this(message, msgId, producerId, txMsgFlag, localTxState.getCode());
    }

    private PushTxMsgReq(Message message, String msgId, String producerId,
                        TxMsgFlagEnum txMsgFlag, int localTxState) {
        super(msgId);
        this.topic = message.getTopic();
        this.queueId = message.getQueueId();
        this.body = message.getBody();
        this.producerId = producerId;
        this.txFlag = txMsgFlag.getCode();
        this.transactionState = localTxState;
    }

    /**
     * 创建事务的半提交消息
     */
    public static PushTxMsgReq newHalfMsg(Message message, String msgId, String producerId) {
        return new PushTxMsgReq(message, msgId, producerId, TxMsgFlagEnum.HALF_MSG, 0);
    }

    /**
     * 创建事务的剩余消息
     */
    public static PushTxMsgReq newRemainMsg(Message message, String msgId, String producerId,
                                            LocalTxStateEnum localTxState) {
        return new PushTxMsgReq(message, msgId, producerId, TxMsgFlagEnum.REMAIN_MSG, localTxState);
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public int getQueueId() {
        return queueId;
    }

    public void setQueueId(int queueId) {
        this.queueId = queueId;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    public int getTxFlag() {
        return txFlag;
    }

    public void setTxFlag(int txFlag) {
        this.txFlag = txFlag;
    }

    public int getTransactionState() {
        return transactionState;
    }

    public void setTransactionState(int transactionState) {
        this.transactionState = transactionState;
    }

    public String getProducerId() {
        return producerId;
    }

    public void setProducerId(String producerId) {
        this.producerId = producerId;
    }
}
